<?php

/**
 * intro actions.
 *
 * @package    SistemaWebUnlSenescyt
 * @subpackage intro
 * @author     JFernando
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class introActions extends sfActions {

    public function executeIndex(sfWebRequest $request) {
        $this->intros = Doctrine_Core::getTable('intro')
                ->createQuery('a')
                ->execute();
    }

    public function executeShow(sfWebRequest $request) {
        $this->intro = Doctrine_Core::getTable('intro')->find(array($request->getParameter('id')));
        $this->forward404Unless($this->intro);
    }

    public function executeNew(sfWebRequest $request) {
        $id = $request->getParameter('id_pro');
        $this->getUser()->setAttribute('id_pro', $id);
        $q = Doctrine_Query::create()
                ->from('proyecto p')
                ->where('p.id_pro=?', $id);
        $this->proyecto = $q->fetchOne();
        $this->form = new introForm();
    }

    public function executeCreate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST));

        $this->form = new introForm();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request) {
        $this->forward404Unless($intro = Doctrine_Core::getTable('intro')->find(array($request->getParameter('id'))), sprintf('Object intro does not exist (%s).', $request->getParameter('id')));
        $this->form = new introForm($intro);
    }

    public function executeUpdate(sfWebRequest $request) {
        $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
        $this->forward404Unless($intro = Doctrine_Core::getTable('intro')->find(array($request->getParameter('id'))), sprintf('Object intro does not exist (%s).', $request->getParameter('id')));
        $this->form = new introForm($intro);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request) {
        $request->checkCSRFProtection();

        $this->forward404Unless($intro = Doctrine_Core::getTable('intro')->find(array($request->getParameter('id'))), sprintf('Object intro does not exist (%s).', $request->getParameter('id')));
        $intro->delete();

        $this->redirect('intro/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form) {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
        if ($form->isValid()) {
            $intro = $form->save();
            //$this->redirect('proyectos/index');
            if ($intro->getNfondos() == "0") {
                $intro->delete();
                $this->getUser()->setFlash('error', 'Usted no ha seleccionado el número de fondos del proyecto!');
                $this->redirect('intro/new?id_pro=' . $this->getUser()->getAttribute('id_pro'));
            } else {
                $va = 0;
                if ($intro->getNfondos() == "1") {
                    $va = 1;
                    $intro->delete();
                    $this->redirect('fondos/new?nu=' . $va);
                } else {
                    if ($intro->getNfondos() == "2") {
                        $va = 2;
                        $intro->delete();
                        $this->redirect('fondos/new?nu=' . $va);
                    } else {
                        $intro->delete();
                        $this->getUser()->setFlash('error', 'Usted no ha seleccionado el número de fondos del proyecto!');
                        $this->redirect('intro/new?id_pro=' . $this->getUser()->getAttribute('id_pro'));
                    }
                }
            }
            //$in = new Intro();
            //$in->getNfondos();
            //$this->redirect('intro/edit?id=' . $intro->getId());
        }
    }

}
